/*
* $Id: ViewQueenBeeWizard.java 275 2011-02-24 23:13:33Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Hashtable;

import apiary.Apiary;
import apiary.common.ConvertFunction;
import apiary.common.FormHelper;
import apiary.db.QueenBeeStore.QueenBeeRecord;
import apiary.db.managers.DBManager;
import apiary.ui.BackScreen;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.ViewForm;
import apiary.ui.journal.WizardScreen;

/**
 * Wizard of view queen bee
 * @author lexus
 */
public class ViewQueenBeeWizard extends Wizard
{
    private ChooseForm familyChoose;

    /**
     * Constructor
     */
    public ViewQueenBeeWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ViewQueenBeeWizard(BackScreen parentForm) {
        super(parentForm);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseQueenBee"),
                                   idChoose, this, true);
        FormHelper.fillChooseQBHList((ChooseForm) firstForm,
                                     apiary.getLocaleString("FreeQueenBee"),
                                     apiary.getLocaleString("ChooseFamily"));

        Integer idChooseF = new Integer(2);
        familyChoose = new ChooseForm(apiary.getLocaleString("ChooseFamily"),
                                      idChooseF, this, true);
        familyChoose.setBackScreen(firstForm);
        FormHelper.fillFamilyList(familyChoose);

        Integer idChooseH = new Integer(3);
        ChooseForm chooseForm = new ChooseForm(
              apiary.getLocaleString("ChooseQueenBee"), idChooseH, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChooseF, chooseForm);
        formsNext.put(idChoose, chooseForm);

        Integer idView = new Integer(4);
        ViewForm form = new ViewForm(apiary.getLocaleString("ViewQueenBee"), idView, this);
        form.setBackScreen(chooseForm);
        formsNext.put(idChooseH, form);
    }

    /**
     * @deprecated In this wizard don't use
     */
    public void save() {
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        if(form.getIdForm().intValue() == 3){
            ChooseValue value = (ChooseValue) getData(new Integer(1));
            int idFamily = 0;
            if(value.id == 1) {
                value = (ChooseValue) getData(new Integer(2));
                idFamily = value.id;
            }
            FormHelper.fillQuennBeeList((ChooseForm) form, idFamily);
        } else if(form.getIdForm().intValue() == 4) {
            ChooseValue value = (ChooseValue) getData(new Integer(3));
            setQueenBeeData((ViewForm) form, value.id);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        if(idForm.intValue() == 1) {
            ChooseValue value = (ChooseValue) dataForm;
            if(value.id == 1) {
                Apiary apiary = Apiary.getInstance();
                apiary.Display(familyChoose);
                setData(idForm, dataForm);
                return false;
            }
        }
        return true;
    }

    /**
     * Form is filled of data of queen bee
     * @param form form
     * @param id identificator of queen bee
     */
    private void setQueenBeeData(ViewForm viewForm, int id) {
        DBManager dBManager = DBManager.getInstance();
        QueenBeeRecord qBRecord = dBManager.getQueenBee(id);
        Apiary apiary = Apiary.getInstance();
        viewForm.append(apiary.getLocaleString("NameQueenBee").concat(": "));
        viewForm.append(qBRecord.nameQueenBee.concat("\n"));
        String date = ConvertFunction.dateToString(qBRecord.date.getTime());
        viewForm.append(apiary.getLocaleString("Birthday").concat(": "));
        viewForm.append(date.concat("\n"));
        String nameParent;
        if(qBRecord.idParentQueenBee != 0) {
            QueenBeeRecord pQBRecord = dBManager.getQueenBee(qBRecord.idParentQueenBee);
            nameParent = pQBRecord.nameQueenBee;
        } else
            nameParent = apiary.getLocaleString("NoParentQB");
        viewForm.append(apiary.getLocaleString("NameParentQB").concat(": "));
        viewForm.append(nameParent.concat("\n"));
        Hashtable breeds = dBManager.getBreeds();
        String breed = (String) breeds.get(new Integer(qBRecord.breed));
        viewForm.append(apiary.getLocaleString("Breed").concat(": "));
        viewForm.append(breed.concat("\n"));
        viewForm.append(apiary.getLocaleString("Fertility").concat(": "));
        viewForm.append(qBRecord.fert.concat("\n"));
        viewForm.append(apiary.getLocaleString("Description").concat(": "));
        viewForm.append(qBRecord.description.concat("\n"));
    }
}
